var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;
var index;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入



        var start = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }

    });
    setTimeout(function () {
        findBusinessShopList();
    },200);

});



//查询商铺列表
function findBusinessShopList(){
    var menuId = $("#menuId").val();
    var label = $("#label").val();
    var state = $("#state").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var phone = $("#phone").val();
    if (phone != "") {
        if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
            alert("请输入有效的手机号码！");
            return false;
        }
    }

    var isPreferredBusiness = "";
    // if($("#s1").val() != "" && $("#s2").val() == ""){
    //     isPreferredBusiness = $("#s1").val();
    // }else if($("#s1").val() == "" && $("#s2").val() != ""){
    //     isPreferredBusiness = $("#s2").val();
    // }
    // var state = $("#s3").val();
    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "state":state,
        "label":label,
        "isPreferredBusiness":isPreferredBusiness,
        "startTime":startTime,
        "endTime":endTime,
        "phone":phone
    };
    $.ajax({
       type:"post",
       url: "/businessShop/findMerchantsList",
       data:params,
       dataType: "json",
       success : function(data){
            if(data.code == 1 && data.code != undefined){
                list = data.authenticationTemps;
                page = data.page;
                var html = "";
                for(var b in list){
                    var state;
                    var label = '';
                    if(list[b].state == 1){
                        state = "待审核";
                    }else if(list[b].state == 2){
                        state = "已审核";
                    }
                    if(list[b].label == 1){
                        label = "美食";
                    }
                    if(list[b].label == 2){
                        label = "美容";
                    }
                    if(list[b].label == 3){
                        label = "休闲食品";
                    }
                    if(list[b].label == 4){
                        label = "生鲜水果";
                    }
                    if(list[b].label == 5){
                        label = "特产";
                    }if(list[b].label == 6){
                        label = "装修家政";
                    }
                    if(list[b].label == 7){
                        label = "房产交易";
                    }
                    if(list[b].label == 8){
                        label = "汽车交易";
                    }

                    // html += '<tr><td><input type="checkbox"></td>' +
                       html += '<tr><td>'+list[b].id+'</td>' +
                        '<td>'+(list[b].businessAbbreviation == null ? "" : list[b].businessAbbreviation)+'</td>' +
                        '<td>'+list[b].phone+'</td>' +
                        '<td>'+list[b].businessTel+'</td>' +
                        '<td>'+list[b].businessIndustry+'</td>' +
                        '<td>'+label+'</td>'+
                        '<td>'+(list[b].auditUserName == null ? "" : list[b].auditUserName)+'</td>'+
                        '<td>'+(list[b].auditTime == null ? "" : new Date(list[b].auditTime).toLocaleString())+'</td>';
                    if(list[b].state == 1 && sessionStorage.getItem(menuId+'edit') == 1){
                        html += '<td><button class="layui-btn layui-btn-normal" onclick="findDetails('+b+')">查看详情</button><button class="layui-btn layui-btn-normal" onclick="showAuditPage('+list[b].id+')">审核</button></td></tr>';
                    }else {
                        html += '<td><button class="layui-btn layui-btn-normal" onclick="findDetails('+b+')">查看详情</button></td></tr>'
                    }

                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findBusinessShopList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }else if(data.code == 2 && data.code != undefined){
                alert(data.message);
            }
       }

    });
}
//条件查询
function ck(obj){
    //按钮样式切换
    if(obj.className == "layui-btn layui-btn-primary"){
        $(obj).attr("class","layui-btn layui-btn-normal");
        if(obj.id == "s1"){
            obj.value = 0;
        }else if(obj.id == "s2"){
            obj.value = 1;
        }else if(obj.id == "s3"){
            obj.value = 1;
        }
    }else {
        $(obj).attr("class","layui-btn layui-btn-primary");
        obj.value = "";
    }
    pageNum = 1;
    findBusinessShopList();
}

//查询按钮
function search() {
    pageNum = 1;
    findBusinessShopList();
}

//查看详情
function findDetails(b){
    // alert(list[b].id);
    var mc = list[b]; //商户信息
    var label = '';
    if(list[b].label == 1){
        label = "美食";
    }else if(list[b].label == 2){
        label = "美容";
    }else if(list[b].label == 3){
        label = "休闲食品";
    }else if(list[b].label == 4){
        label = "生鲜水果";
    }else if(list[b].label == 5){
        label = "特产";
    }else if(list[b].label == 6){
        label = "装修家政";
    }else if(list[b].label == 7){
        label = "房产交易";
    }else if(list[b].label == 8){
        label = "汽车交易";
    }

    var orderHtml = '<div style="padding-left: 18px"><strong>基本信息:</strong><br/>'+
        '<table style="width: 100%;border-collapse: separate;border-spacing: 10px"><tr><td style="width: 80px">商户简称:</td><td style="text-align: left">'+(mc.businessAbbreviation == null ? "" : mc.businessAbbreviation)+'</td><td style="width: 80px">商户全称:</td><td style="text-align: left">'+mc.businessFullName+'</td></tr>'+
        '<tr><td style="width: 80px">注册手机号:</td><td style="text-align: left">'+mc.phone+'</td><td style="width: 80px">商户电话:</td><td style="text-align: left">'+mc.businessTel+'</td></tr>'+
        '<tr><td style="width: 80px">行业:</td><td style="text-align: left">'+mc.businessIndustry+'</td><td style="width: 80px">主营业务:</td><td style="text-align: left">'+label+'</td></tr>'+
        '<tr><td style="width: 80px">法人身份证:</td><td style="text-align: left">'+mc.identityCard+'</td><td style="width: 80px">省市区:</td><td style="text-align: left">'+mc.province+'-'+mc.city+'-'+mc.area+'</td></tr>'+
        '<tr><td style="width: 80px">商户地址:</td><td style="text-align: left">'+mc.businessAddress+'</td></tr>'+
        '<tr><td style="width: 90px">商户营业执照:</td><td style="text-align: left"><img src="'+mc.bussinessLicencePhoto+'" style="width: 150px;"></td><td style="width: 80px">手持照片:</td><td style="text-align: left"><img src="'+mc.businessCredentialsUp+'" style="width: 150px"></td></tr></table>'+
        '<strong>店铺主页信息:</strong><br/>';


    var menuId = $("#menuId").val();
    var params = {
        "type":"query",
        "menuId":menuId,
        "userId":list[b].userId
    };
    $.ajax({
        type:"post",
        url:"/businessShop/findBusinessShopByUserId",
        data:params,
        dataType:"json",
        success: function (data) {
            if(data.code == 1 && data.code != undefined){
                var businessShop = data.businessShop;  //商铺信息


                var firstPic = businessShop.firstPic.split(";") //图片信息
                var secondPic = businessShop.secondPic.split(";") //图片信息
                var thirdPic = businessShop.thirdPic.split(";") //图片信息

                orderHtml += '<table style="width: 100%;border-collapse: separate;border-spacing: 10px"><tr><td style="width: 80px">店铺简介:</td><td style="text-align: left">'+businessShop.about+'</td></tr>'+
                    '<tr><td style="width: 90px">商铺招牌图片:</td><td style="text-align: left"><img src="'+businessShop.shopSign+'" style="width: 100px;"></td></tr>';
                if(businessShop.firstVideo != null && businessShop.firstVideo != "" && businessShop.firstVideo != undefined){
                    var videoInfo = businessShop.firstVideo.split(";");//视频信息
                    orderHtml +=  '<tr><td style="width: 90px">视频标题:</td><td style="text-align: left">'+videoInfo[1]+'</td><td style="width: 90px">视频:</td><td style="text-align: left"><video src="'+videoInfo[0]+' " controls="controls" style="width: 200px;height: 200px"></video></td><td style="width: 90px">视频链接:</td><td style="text-align: left"><a href="'+videoInfo[0]+'">'+videoInfo[0]+'</a></td></tr>';
                }else {
                    orderHtml +=  '<tr><td style="width: 90px">视频标题:</td><td style="text-align: left"></td><td style="width: 90px">视频:</td><td style="text-align: left"><video src="" controls="controls" style="width: 200px;height: 200px"></video></td><td style="width: 90px">视频链接:</td><td style="text-align: left"><a href=""></a></td></tr>';
                }

                orderHtml += '<tr><td style="width: 90px">图片:</td><td style="text-align: left"><img src="'+firstPic[0]+'" style="width: 100px;height: 100px;"></td>'+
                    '<td style="text-align: left"><img src="'+secondPic[0]+'" style="width: 100px;height: 100px;"></td>'+
                    '<td style="text-align: left"><img src="'+thirdPic[0]+'" style="width: 100px;height: 100px;"></td></tr>';

                if(businessShop.activity != null && businessShop.activity != "" && businessShop.activity != undefined){
                    var activity = JSON.parse(businessShop.activity); //活动信息
                    for(var i=0;i<activity.length;i++){
                        orderHtml += '<tr><td style="width: 90px">活动标题:</td><td style="text-align: left">'+activity[i].actionTitle+'</td>'+
                            '<td style="width: 90px">活动介绍:</td><td style="text-align: left">'+activity[i].actionDesc+'</td>'+
                            '<td style="width: 90px">活动图片:</td><td style="text-align: left"><img src="'+activity[i].actionImageUrl+'" style="width: 100px"></td></tr>';
                    }
                }

            }
            orderHtml += '</table></div>';
            layer.open({
                title: ['商家详情','color: #2299ee;border-bottom: 1px solid;']
                ,type: 1
                ,content: orderHtml
                ,area: ['90%', '80%']
                ,maxmin:true
            });
        },
    });
}
//打开审核页面
function showAuditPage(id) {
    var html = "<textarea id='content' style='width: 497px;height: 180px;resize: none'></textarea><button id='bt1' onclick='through("+id+","+1+",this)' class='layui-btn layui-btn-normal' style='margin-left: 100px !important;'>通过</button><button id='bt2' onclick='through("+id+","+2+",this)' class='layui-btn layui-btn-danger' style='margin-left: 100px !important;'>不通过</button>"
    index = layer.open({
        title: ['商家审核','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        // ,btn:['通过','不通过']
        // ,btn1:function () {
        //     through(id,1,$("#content").val());
        // }
        // ,btn2:function () {
        //     through(id,2,$("#content").val());
        // }
        ,content: html
        ,area: ['500px', '300px']
        ,maxmin:true
    });
}
//审核通过
function through(id,auditType,obj) {
    $("#bt1").attr("disabled",true);
    $("#bt1").attr("class","layui-btn layui-btn-disabled");
    $("#bt2").attr("disabled",true);
    $("#bt2").attr("class","layui-btn layui-btn-disabled");
    $(obj).attr("disabled",true);
    var content =$("#content").val();
    if(auditType == 2){
        if(content == "" || content == undefined){
            alert("请填写审核信息");
            $(obj).attr("disabled",false);
            $(obj).attr("class","layui-btn layui-btn-danger");
            $("#bt1").attr("disabled",true);
            $("#bt1").attr("class","layui-btn layui-btn-normal");
            return false;
        }
    }
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id,
        "auditType":auditType,
        "content":content
    };
    $.ajax({
        type:"post",
        url:"/businessShop/merchantsAudit",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("审核成功");
                layer.close(index);
                findBusinessShopList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                layer.close(index);
                findBusinessShopList();
            }else if(data.code == 2 && data.code != undefined){
                alert("审核失败");
                layer.close(index);
                findBusinessShopList();
            }else if(data.code == 3 && data.code != undefined){
                alert("审核失败");
                layer.close(index);
                findBusinessShopList();
            }
        }
    });
}
